package com.timeriver.web;

import com.timeriver.domain.ResponseObj;
import com.timeriver.domain.dto.SparkModelDto;
import com.timeriver.domain.vo.ModelsVO;
import com.timeriver.domain.vo.ModelVO;
import com.timeriver.domain.vo.UploadFileVO;
import com.timeriver.domain.vo.VersionVO;
import com.timeriver.service.ModelService;
import com.timeriver.utils.EntityConvertUtil;
import com.timeriver.utils.ResponseUtil;
import com.timeriver.utils.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 算法模型文件上传与解压功能
 */
@RestController
@RequestMapping("/model")
public class ModelController {

    @Autowired
    private ModelService modelService;

    @Value("${model.parentPath}")
    private String modelPath;

    @PostMapping("/file")
    public ResponseObj saveModelInfo(@RequestBody ModelVO sparkModel) {
        try {
            SparkModelDto modelDto = EntityConvertUtil.object2DtoOrEntity(sparkModel, SparkModelDto.class);
            modelDto.setModelPath(modelPath + UUIDUtil.getTimestampUUID("model", "-"));
            modelService.saveModelInfo(modelDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error(e.getMessage());
        }
        return ResponseUtil.success();
    }

    @GetMapping("/list")
    public ResponseObj getModelList(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                    @RequestParam(value = "size", defaultValue = "12") Integer size,
                                    @RequestParam(value = "search", required = false) String search) {
        ModelsVO models = null;
        try {
            if (StringUtils.isEmpty(search)) {
                models = modelService.getModelList(PageRequest.of(page, size));
            } else {
                models = modelService.getModelList(PageRequest.of(page, size), search);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error(e.getMessage());
        }

        return ResponseUtil.success(models);
    }

    @DeleteMapping("/file/{id}")
    public ResponseObj deleteModel(@PathVariable String id) {
        try {
            modelService.deleteModel(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("删除失败！");
        }
        return ResponseUtil.success();
    }

    @PostMapping("/upload")
    public ResponseObj uploadFile(UploadFileVO uploadFile) {
        try {
            modelService.uploadFile(uploadFile);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error(e.getMessage());
        }
        return ResponseUtil.success();
    }

    @GetMapping("/versions/{id}")
    public ResponseObj getModelVersions(@PathVariable String id) {
        List<VersionVO> modelVersion = null;
        try {
            modelVersion = modelService.getModelVersion(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error(e.getMessage());
        }
        return ResponseUtil.success("获取版本文件成功!", modelVersion);
    }

    @GetMapping("/version")
    public ResponseObj setVersion(@RequestParam String id,
                                  @RequestParam Integer version) {
        Integer versionNum = null;
        try {
            versionNum = modelService.setModelVersion(id, version);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error(e.getMessage());
        }

        return ResponseUtil.success(versionNum);
    }
}
